<?php defined('RUN_KATANA') or die('Restricted access'); 
if($shop->getOrders())
{
	$sum['items'] = 0;
	$sum['items_vat'] = 0;
	$sum['items_total'] = 0;
	$sum['shipping'] = 0;
	$sum['order_total'] = 0;
	$out = '';
	
	$dateStart = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$dateEnd = mktime(0,0,0,date('m'),date('t'),date('Y'));
	
	if($_REQUEST['date_start'] != '')
	{ 
		$year = $shop->dateTimeExtract($_REQUEST['date_start'], 'year');
		$month = $shop->dateTimeExtract($_REQUEST['date_start'], 'month');
		$day = $shop->dateTimeExtract($_REQUEST['date_start'], 'day');
		if(($day > 0) && ($month > 0) && ($year > 0))
		{ $dateStart = mktime(0,0,0,$month,$day,$year); }
	}
	if($_REQUEST['date_end'] != '')
	{ 
		$year = $shop->dateTimeExtract($_REQUEST['date_end'], 'year');
		$month = $shop->dateTimeExtract($_REQUEST['date_end'], 'month');
		$day = $shop->dateTimeExtract($_REQUEST['date_end'], 'day');
		if(($day > 0) && ($month > 0) && ($year > 0))
		{ $dateEnd = mktime(0,0,0,$month,$day,$year); }
	}
	
	foreach($shop->getOrders() as $order)
	{
		$year = $order->dateTimeExtract($order->getDateDelivery(), 'year');
		$month = $order->dateTimeExtract($order->getDateDelivery(), 'month');
		$day = $order->dateTimeExtract($order->getDateDelivery(), 'day');
		
		if(($day > 0) && ($month > 0) && ($year > 0))
		{ $dateOrder = mktime(0,0,0,$month,$day,$year); }
		
		if((($order->getDeliveryStatus() == $_GET['status']) || ($_GET['status'] == '')) && (($dateStart <= $dateOrder) && ($dateOrder <= $dateEnd)))
		{
			$out .= '<tr class="items">';
				$out .= '<td class="first center">'.substr($order->getDateDelivery(), 0, 10).'</td>';
				$out .= '<td>'.substr($order->getDateOrdered(), 0, 10).'</td>';
				$out .= '<td>';
				$out .= $order->getId();
				$out .= '</td>';
				$out .= '<td>';
					if($order->getShippingZoneCode() != '')
					{ $out .= $order->getShippingZoneCode(); }
				$out .= '</td>';
				$out .= '<td>';
					if($order->getDeliveryZip(4) != '')
					{ $out .= $order->getDeliveryZip(4); }
				$out .= '</td>';
				/*$out .= '<td>';
					if($order->getDeliveryCity() != '')
					{ $out .= ucfirst($order->getDeliveryCity()); }
				$out .= '</td>';*/
				$out .= '<td class="right">'.number_format($order->getCostItems(2),2,',',' ').'</td>';
				$sum['items'] += $order->getCostItems();
				$out .= '<td class="right">'.number_format($order->getCostItemsVat(2),2,',',' ').'</td>';
				$sum['items_vat'] += $order->getCostItemsVat();
				$out .= '<td class="right">'.number_format($order->getCostItemsTotal(1),2,',',' ').'</td>';
				$sum['items_total'] += $order->getCostItemsTotal();
				$out .= '<td class="right">'.number_format($order->getCostShipping(2),2,',',' ').'</td>';
				$sum['shipping'] += $order->getCostShipping(2);
				$out .= '<td class="last right">'.number_format($order->getCostTotal(1),2,',',' ').'</td>';
				$sum['order_total'] += $order->getCostTotal();
			$out .= '</tr>';
		}
	}
	if(OUTPUT_FORMAT != 'csv')
	{
		$out .= '<tr class="items">';
			$out .= '<td class="first center nosort"></td>';
			$out .= '<td></td>';
			$out .= '<td></td>';
			$out .= '<td></td>';
			$out .= '<td></td>';
			//$out .= '<td></td>';
			$out .= '<td class="right underline nosort"><b>'.number_format($sum['items'],2).'</b></td>';
			$out .= '<td class="right underline nosort"><b>'.number_format($sum['items_vat'],2,',',' ').'</b></td>';
			$out .= '<td class="right underline nosort"><b>'.number_format($sum['items_total'],2,',',' ').'</b></td>';
			$out .= '<td class="right underline nosort"><b>'.number_format($sum['shipping'],2,',',' ').'</b></td>';
			$out .= '<td class="last right underline nosort"><b>'.number_format($sum['order_total'],2,',',' ').'</b></td>';
		$out .= '</tr>';
	}
	
	if(OUTPUT_FORMAT == 'csv')
	{
		$headers  = ucfirst($txt->getWord('delivery_date')).';';
		$headers .= ucfirst($txt->getWord('order_date')).';';
		$headers .= ucfirst($txt->getWord('order_id')).';';
		$headers .= ucfirst($txt->getWord('shipping').' '.$txt->getWord('zone')).';';
		$headers .= ucfirst($txt->getWord('zip')).';';
		//$headers .= ucfirst($txt->getWord('city')).';';
		$headers .= ucfirst($txt->getWord('sum').' '.$txt->getWord('products')).';';
		$headers .= ucfirst($txt->getWord('sum').' '.$txt->getWord('vat')).';';
		$headers .= ucfirst($txt->getWord('full').' '.$txt->getWord('price')).';';
		$headers .= ucfirst($txt->getWord('sum').' '.$txt->getWord('shipping')).';';
		$headers .= ucfirst($txt->getWord('order_total')).';';

		$out = str_replace('<tr class="items">','',$out);
		$out = str_replace('<tr>','',$out);
		$out = str_replace('<td>','',$out);
		$out = str_replace('</td>',';',$out);
		$out = str_replace('<td class="first center">','',$out);
		$out = str_replace('<td class="last right">','',$out);
		$out = str_replace('<td class="right">','',$out);
		
		$csvLines = explode("</tr>",$out);
		if(count($csvLines > 0))
		{
			$filename = "report_".$_GET['report'].date("Y-m-d_H-i").'.csv';
			$file = fopen(FILES_PATH.'reports/'.$filename,"w");
			
			fputcsv($file, explode(";",$headers), ';');
			
			foreach($csvLines as $line)
			{
				$csvLine = explode(";",$line);
				fputcsv($file, $csvLine, ';');
			}
			fclose($file);
			$out = '<a target="_blank" href="'.FILES_URL.'reports/'.$filename.'">'.ucfirst($txt->getWord('download')).'<br />'.$filename.'</a>';
		}
	}
	echo $out;
}
?>